let $add_x = $('#add_cart').offset().top;
let $add_y = $('#add_cart').offset().left;

let $to_x = $('#show_count').offset().top;
let $to_y = $('#show_count').offset().left;

$(".add_jump").css({
    'left': $add_y + 80,
    'top': $add_x + 10,
    'display': 'block'
});

let vm = new Vue({
    el: "#app",
    data: {
        count: 0,
        search: "", // 搜索关键字
        tabIndex: 0, // 选项卡下标
        productId: "",
        isLogin: false,
        user: {},
        cart: { // 购物车
            productId: "",
            money: "",
            quantity: 1
        },
        product: {}, // 商品
        comments: [], // 商品的评论
        recommends: [], // 新品推荐
        categoryArr: [] // 全部商品类型
    },
    created: function () {
        let param = location.search.split("=")[1];

        if (param !== undefined) {
            this.productId = param;

            axios.get("/product/selectById", {
                params: {
                    id: param
                }
            }).then(function (response) {
                vm.product = response.data;

                // 设置购物车中商品id和金额
                vm.cart.productId = vm.product.id;
                vm.cart.money = ((vm.product.price * 100) * vm.cart.quantity) / 100;
            });
        }

        // 获取登录的用户信息
        axios.get("/user/getlogin").then(function (response) {
            vm.user = response.data;
            vm.isLogin = response.data !== "";
        });

        // 全部商品评论
        axios.get("/comment/selectByProductId?productId=" + this.productId).then(function (response) {
            vm.comments = response.data;
        });

        // 全部商品分类
        axios.get("/category/selectAll").then(function (response) {
            vm.categoryArr = response.data;
        });

        // 购物车里的商品数量
        axios.get("/shop_cart/selectCount").then(function (response) {
            vm.count = response.data;
        });

        // 新品推荐
        axios.get("/product/selectNew").then(function (response) {
            vm.recommends = response.data;
        });
    },
    methods: {
        select() {
            location.href = "/list.html?name=" + vm.search;
        },
        plus() { // 加
            if (vm.cart.quantity + 1 <= vm.product.quantity) {
                vm.cart.quantity ++;
            }
        },
        subtract() { // 减
            if (vm.cart.quantity > 1) {
                vm.cart.quantity --;
            }
        },
        buy() { // 购买
            // 添加到购物车
            axios.post("/shop_cart/insert", vm.cart).then(function (response) {
                let result = response.data;

                if (result.code === 200) {
                    location.href = "/html/place_order.html?cart_id=" + result.data;
                } else {
                    alert(result.msg);
                }
            });
        },
        show(index) {
            vm.tabIndex = index;
        },
        toCart() { // 加入购物车
            $(".add_jump").stop().animate({
                'left': $to_y + 7,
                'top': $to_x + 7
            }, "fast", function () {
                // 添加到购物车
                axios.post("/shop_cart/insert", vm.cart).then(function (response) {
                    let result = response.data;

                    if (result.code === 200) {
                        // 重新查询购物车里的商品数量
                        axios.get("/shop_cart/selectCount").then(function (response) {
                            $(".add_jump").fadeOut('fast', function () {
                                vm.count = response.data;
                            });
                        });
                    } else {
                        alert(result.msg);
                    }
                });
            });
        },
        logout() {
            axios.get("/user/logout").then(function (response) {
                if (response.data.code === 200) {
                    location.href = "/login.html";
                } else {
                    alert(response.data.msg);
                }
            });
        }
    }
});